$(function() {
	// Suppression d'un menu
	$('a.js-del-menu').click(function() {
		$(this).closest('div').html('<img src="images/ajax-loader.gif" alt="En cours..." />');
		var id = $(this).attr('id').split('_')[1];
		var idmenu = $(this).attr('id').split('_')[2];
		
		$.ajax({
			url: 'pages_admin/ajax_admin.php',
			type: 'GET',
			data: 'action=menubloc_delete'
				+ '&id=' + id
				+ '&idmenu=' + idmenu,
			
			success: function(result) {
				result = result.split('&&&')[0];
				if (result == 'ok') {
					$('#ligne' + id).closest('tr').animate({
						'backgroundColor': '#ff8888',
						'color': '#941010'
					}, 500);
					$('#ligne' + id).closest('tr').fadeOut();
				}
				else {
					$('div.ajax').html('<p class="error">Erreur lors de la suppression</p>');
				}
			}
		});
		
		return false;
	});

	// Ajout de blocs et menus
	$('select#type').change(function() {
		$('div.hidden, div.ajax').hide();
		$('input[type=text]').val('');
		$('span#selected_icon').html('');
		
		var type = $(this).val();
		if(type != -1) {
			$('div#'+type).fadeIn('slow');
			$('h3 span').html(type);
		}
	});
	
	$('div#icons_list img').click(function() {
		var image_name = $(this).attr('alt');
		var image = $(this).attr('src');
		$('input[name=image]').val(image_name);
		$('span#selected_icon').html('<img src="'+image+'" alt="selected_icon" />');
	});
	
	$('input.record').click(function() {
		$('div.ajax').show();
		$('div.ajax').html('<img src="images/ajax-loader.gif" alt="En cours..." />');
		
		var action = $(this).attr('id');
		var name = $('input[name=name]:visible').val(); name = escape(name);
		var image = $('input[name=image]').is(':visible') ? $('input[name=image]').val() : -1;
		var idmenu = $('select[name=section]').is(':visible') ? $('select[name=section]').val() : -1;
		
		$.ajax({
			url: 'pages_admin/ajax_admin.php',
			type: 'GET',
			data: 'action=' + action
				+ '&name=' + name
				+ '&image=' + image
				+ '&idmenu=' + idmenu,
			
			success: function(result) {
				result = result.split('&&&');
				if (result[0] == 'ok' || result[0] == 'errors') {
					$('div.ajax').html(result[1]);
				}
				else {
					$('div.ajax').html('<p class="error">Erreur lors de l\'enregistrement</p>');
				}
			}
		});
		
		return false;
	});

	// Réorganisation de l'ordre des menus
	$('ul.sortable').sortable({
		placeholder: 'ui-state-highlight',
		connectWith: '.connectedSortable',
		update: function(event, ui) {
			var idsec = $(this).parent().attr('id');
			idsec = idsec.split('_')[1];
			$(this).children('li').each(function() {
				var myid = this.id;
				myid = myid.split('_')[1];
				myid = myid.split('sec')[0];
				this.id = 'menus_' + myid + 'sec' + idsec;
			});
		}
	}).disableSelection();

	$('ul#all').sortable({
		placeholder: 'ui-state-highlight',
	}).disableSelection();
	
	$('input#cancel_positions').click(function() {
		window.location.reload();
	});
	
	$('input#save_positions').click(function() {
		$('span#pos_update_loading').html('<img src="images/ajax-loader-min.gif" alt="En cours..." />');
		
		var tabsections = $('ul#all').sortable('toArray');
		var nbsections = tabsections.length;
		
		var sections = $('ul#all').sortable('serialize');
		var menus = '';
		
		for(var i=0 ; i < nbsections ; i++) {
			if(i != 0) menus += '&';
			menus += $('ul#list_' + i).sortable('serialize');
		}
		
		$.ajax({
			url: 'pages_admin/ajax_admin.php',
			type: 'GET',
			data: 'action=menu_update_position&'
				+ sections + '&'
				+ menus,
			
			success: function(result) {
				result = result.split('&&&')[0];
				if (result == 'ok') {
					$('span#pos_update_loading').html('<img src="images/icons/accept.png" alt="OK!" />');
				}
				else {
					$('span#pos_update_loading').html('<img src="images/icons/exclamation.png" alt="Error!" />');
				}
			}
		});
	});
	
	// Renomage des menus
	$('span.menu_name, span.menu_section_name').click(function() {
		if(!($('span').is(':has(input)'))) {
			$('span.loading').html('');
			var id = $(this).closest('li').attr('id'); id = id.split('_')[1];
			var name = $(this).html();
			$(this).html('<input type="text" id="'+id+'" value="'+name+'" /><input id="valid_update" type="submit" value="OK" />');
		}
	});
	
	$('input#valid_update').live('click', function() {
		$('span.loading').html('');
		$(this).parent().next('span.loading').html('<img src="images/ajax-loader-min.gif" alt="En cours..." />');
		
		var new_menu_name = $('input[type=text]:visible').val();
		var id = $('input[type=text]').attr('id');
		
		$.ajax({
			url: 'pages_admin/ajax_admin.php',
			type: 'GET',
			data: 'action=menu_update_name'
				+ '&id=' + id
				+ '&new_menu_name=' + new_menu_name,
			
			success: function(result) {
				result = result.split('&&&')[0];
				if (result == 'ok') {
					$('input#valid_update').parent().html(new_menu_name);
					$('span.loading:has(img)').html('<img src="images/icons/accept.png" alt="OK!" />');
				}
				else {
					$('span.loading:has(img)').html('<img src="images/icons/exclamation.png" alt="Error!" />');
				}
			}
		});
		
	});
});
